package com.busdepot.webapp.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class DbUtils {
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/busdepot", "root", "");
	}
	
	public static SimpleJdbcTemplate getSimpleJdbcTemplate(){
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/busdepot");
		dataSource.setUsername("root");
		dataSource.setPassword("");
		return new SimpleJdbcTemplate(dataSource);
	}
	
	public static void close(Connection conn, Statement stmt, ResultSet rs){
		if (rs != null){
			try{rs.close();
			}
			catch (SQLException ignore){}
		}
		if (stmt !=null){
			try{stmt.close();
			}
			catch (SQLException ignore){}
			}
		
		if (conn != null) {
			try{conn.close();
			}
			catch (SQLException ignore){}
		}
	}


public static void convertCheckedExceptionToRuntime(Exception e){
	e.printStackTrace();
	throw new RuntimeException(e.getClass().getName() + " : " + e.getMessage());
	}
}